import 'package:fish_redux/fish_redux.dart';
import 'model/order_view_model.dart';
import 'action.dart';
import 'state.dart';

Reducer<OrderViewState> buildReducer() {
  return asReducer(
    <Object, Reducer<OrderViewState>>{
      OrderViewAction.action: _onAction,
      OrderViewAction.onGetData: _onGetData,
      OrderViewAction.onSetConfigData: _onSetConfigData,
      OrderViewAction.onSetNormalData: _onSetNormalData,
      OrderViewAction.onTapOther: _onTapOther,
    },
  );
}

OrderViewState _onAction(OrderViewState state, Action action) {
  final OrderViewState newState = state.clone();
  return newState;
}

OrderViewState _onGetData(OrderViewState state, Action action) {
  final OrderViewState newState = state.clone()..items = action.payload;
  return newState;
}

OrderViewState _onSetConfigData(OrderViewState state, Action action) {
  final OrderViewState newState = state.clone();
  OrderConfigModel model = action.payload['model'];
  ConfigValue data = action.payload['data'];
  if (model.values.contains(data)) {
    model.values.remove(data);
  } else {
    model.values.add(data);
  }
  model.isNormal = false;
  if (model.isRemind)
    model.isNormal = true;
  return newState;
}

OrderViewState _onSetNormalData(OrderViewState state, Action action) {
  final OrderViewState newState = state.clone();
  OrderConfigModel model = action.payload;
  model.isNormal = true;
  model.hasOther = false;
  model.values.clear();
  model.imageList.clear();
  model.textEditingController.clear();
  return newState;
}

OrderViewState _onTapOther(OrderViewState state, Action action) {
  final OrderViewState newState = state.clone();
  OrderConfigModel model = action.payload;
  model.hasOther = !model.hasOther;
  if (model.hasOther) model.isNormal = false;
  return newState;
}
